/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.thingsgrid.notice.service.impl;

import com.alibaba.fastjson.JSON;
import com.thingsgrid.notice.entity.NoticeEntiy;
import com.thingsgrid.notice.entity.SendEntiy;
import com.thingsgrid.notice.exception.NoticeException;
import com.thingsgrid.notice.repository.ICommonRepository;
import com.thingsgrid.notice.service.INoticeService;
import com.thingsgrid.notice.service.ISendService;
import com.thingsgrid.notice.service.InsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Slf4j
@AllArgsConstructor
@Service
public class NsServiceImpl implements InsService<NoticeEntiy,SendEntiy> {

    private ISendService<SendEntiy> iSendService;
    private ICommonRepository iCommonRepository;
    private INoticeService<NoticeEntiy> iNoticeService;
    @Override
    @Transactional(rollbackFor=Exception.class)
    public NoticeEntiy save(NoticeEntiy noticeEntiy, SendEntiy sendEntiy) {
        try {
            noticeEntiy = iNoticeService.save(noticeEntiy);
            sendEntiy.setMessageTextId(noticeEntiy.getId());
            iSendService.save(sendEntiy);
            return noticeEntiy;
        }catch (Exception e){
            throw new ServiceException("发布消息失败");
        }

    }

    @Override
    public List<NoticeEntiy> findByNames(String findName, String findVal) {
        try{
            List<SendEntiy> lists = iSendService.queryLikeByReceiverName(findVal);
            List<String> listId = new ArrayList<>();
            lists.forEach(v->listId.add(v.getMessageTextId()));
            return iNoticeService.findNoticeByName("id",listId);
        }catch (Exception e){
            throw new NoticeException(R.fail("根据ID查询失败"));
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public R deleteByIds(String ids) {
        try{
			String[] split = ids.split(",");
			List<String> listId = Arrays.asList(split);
			//List<String> listId = JSON.parseArray(ids,String.class);
            boolean nd = iCommonRepository.batchDeleteByName(NoticeEntiy.class,listId,"_id");
            boolean sd = iCommonRepository.batchDeleteByName(SendEntiy.class,listId,"message_text_id");
            if(nd&&sd) {
                return R.success("OK");
            }else {
                throw new NoticeException(R.fail("根据IDS批量删除失败"));
            }
        }catch (Exception e){
            throw new NoticeException(R.fail("根据IDS批量删除失败"));
        }
    }
    @Override
    @Transactional(rollbackFor=Exception.class)
    public R deleteAll(String receiverName) {
        try{
            List<String> list = Arrays.asList(receiverName);
            List<SendEntiy> listSendEntitys = iCommonRepository.queryByName(new SendEntiy(),"receiver_name",receiverName);
            List<String> listId = new ArrayList<>();
            listSendEntitys.forEach(v->listId.add(v.getMessageTextId()));
            iCommonRepository.batchDeleteByName(NoticeEntiy.class,listId,"_id");
            iCommonRepository.batchDeleteByName(SendEntiy.class,list ,"receiver_name");
            return R.success("OK");
        }catch (Exception e){
            throw new NoticeException(R.fail("全部删除失败"));
        }

    }
}
